class SRWeaponPickup extends KFWeaponPickup
	abstract;

var		bool	bAltAttackSameAmmo;

static function GetWeaponStatsTraderInfo(Actor RelatedActor, class<WeaponPickup> PickupClass, out float Power, out float Speed, out float Range)
{
	local class<KFWeaponPickup> KFP;
	local class<SRWeaponPickup> SRP;
	local SRPlayerReplicationInfo SRPRI;
	local class<SRVeterancy> SRVet;
	local class<WeaponFire> ThisFireClass;
	local int CurrentPerkIndex;
	
	SRPRI = SRPlayerReplicationInfo(RelatedActor);
	
	if ( SRPRI != none )
	{
		SRVet = class<SRVeterancy>(SRPRI.ClientVeteranSkill);
	}
	else
	{
		SRVet = class'SRVeterancy';
	}
	
	KFP = class<KFWeaponPickup>(PickupClass);
	
	if ( SRVet.Static.GetPickupShopPerkIndex(SRPRI,PickupClass) != SRVet.default.PerkIndex )
	{
		CurrentPerkIndex = KFP.default.CorrespondingPerkIndex;
	}
	else
	{
		CurrentPerkIndex = SRVet.default.PerkIndex;
	}
	
	Power = GetWeaponDamagePercent(RelatedActor,CurrentPerkIndex,PickupClass);
	Speed = GetWeaponSpeedPercent(RelatedActor,CurrentPerkIndex,PickupClass);
	Range = GetWeaponRangePercent(RelatedActor,CurrentPerkIndex,PickupClass);
}

static function float GetWeaponDamagePercent(Actor RelatedActor, int CurrentPerkIndex, class<WeaponPickup> PickupClass)
{
	local int i,n;
	local Mutator Mut;
	local class<SRVeterancy> SRVet;
	local array< class<Weapon> > PerkWeapons;
	local bool bNeedLoad;
	local float MaxDamage;
	
	for(Mut=RelatedActor.Level.Game.BaseMutator; Mut!=none; Mut=Mut.NextMutator)
	{
		if ( ServerPerksMut(Mut) != none )
		{
			break;
		}
	}
	
	n = ServerPerksMut(Mut).LoadPerks.Length;
	
	for(i=0; i<n; i++)
	{
		SRVet = ServerPerksMut(Mut).LoadPerks[i];

		if ( SRVet.default.PerkIndex == CurrentPerkIndex )
		{
			break;
		}
	}
	
	n = class'DKUtil'.default.AvailableWeapons.Length;
	
//	SRVet = GetWeaponPerkFunctionalityType(SRVet,PickupClass);
	
	for(i=0; i<n; i++)
	{
		bNeedLoad = false;

		if ( SRVet.default.PerkIndex != GetWeaponPerkFunctionalityType(SRVet,PickupClass).default.PerkIndex )
		{
		//	bNeedLoad = false;
		}
		else if ( CurrentPerkIndex == class<KFWeaponPickup>(class'DKUtil'.default.AvailableWeapons[i]).default.CorrespondingPerkIndex )
		{
			bNeedLoad = true;
		}
		else if ( SRVet.Static.GetPickupShopPerkIndex(SRPlayerReplicationInfo(RelatedActor),class'DKUtil'.default.AvailableWeapons[i]) == CurrentPerkIndex )
		{
			bNeedLoad = true;
		}
		
		if ( bNeedLoad )
		{
			PerkWeapons.Insert(0,1);
			PerkWeapons[0] = class<Weapon>(class'DKUtil'.default.AvailableWeapons[i].default.InventoryType);
		}
	}
	
	MaxDamage = 0.00;
	
	while ( PerkWeapons.Length > 0 )
	{
		MaxDamage = FMax(MaxDamage,GetWeaponDamage(PerkWeapons[0],SRVet));
		PerkWeapons.Remove(0,1);
	}
	
	return GetWeaponDamage(class<Weapon>(PickupClass.default.InventoryType),SRVet) / MaxDamage;
}

static function float GetWeaponSpeedPercent(Actor RelatedActor, int CurrentPerkIndex, class<WeaponPickup> PickupClass)
{
	local int i,n;
	local Mutator Mut;
	local class<SRVeterancy> SRVet;
	local array< class<Weapon> > PerkWeapons;
	local bool bNeedLoad;
	local float MaxSpeed;
	
	for(Mut=RelatedActor.Level.Game.BaseMutator; Mut!=none; Mut=Mut.NextMutator)
	{
		if ( ServerPerksMut(Mut) != none )
		{
			break;
		}
	}
	
	n = ServerPerksMut(Mut).LoadPerks.Length;
	
	for(i=0; i<n; i++)
	{
		SRVet = ServerPerksMut(Mut).LoadPerks[i];

		if ( SRVet.default.PerkIndex == CurrentPerkIndex )
		{
			break;
		}
	}
	
	n = class'DKUtil'.default.AvailableWeapons.Length;
	
//	SRVet = GetWeaponPerkFunctionalityType(SRVet,PickupClass);
	
	for(i=0; i<n; i++)
	{
		bNeedLoad = false;

		if ( SRVet.default.PerkIndex != GetWeaponPerkFunctionalityType(SRVet,PickupClass).default.PerkIndex )
		{
		//	bNeedLoad = false;
		}
		if ( CurrentPerkIndex == class<KFWeaponPickup>(class'DKUtil'.default.AvailableWeapons[i]).default.CorrespondingPerkIndex )
		{
			bNeedLoad = true;
		}
		else if ( SRVet.Static.GetPickupShopPerkIndex(SRPlayerReplicationInfo(RelatedActor),class'DKUtil'.default.AvailableWeapons[i]) == CurrentPerkIndex )
		{
			bNeedLoad = true;
		}
		
		if ( bNeedLoad )
		{
			PerkWeapons.Insert(0,1);
			PerkWeapons[0] = class<Weapon>(class'DKUtil'.default.AvailableWeapons[i].default.InventoryType);
		}
	}
	
	MaxSpeed = 0.00;
	
	while ( PerkWeapons.Length > 0 )
	{
		MaxSpeed = FMax(MaxSpeed,GetWeaponSpeed(PerkWeapons[0],SRVet));
		PerkWeapons.Remove(0,1);
	}
	
	return GetWeaponSpeed(class<Weapon>(PickupClass.default.InventoryType),SRVet) / MaxSpeed;
}

static function float GetWeaponRangePercent(Actor RelatedActor, int CurrentPerkIndex, class<WeaponPickup> PickupClass)
{
	local int i,n;
	local Mutator Mut;
	local class<SRVeterancy> SRVet;
	local array< class<Weapon> > PerkWeapons;
	local bool bNeedLoad;
	local float MaxRange;
	
	for(Mut=RelatedActor.Level.Game.BaseMutator; Mut!=none; Mut=Mut.NextMutator)
	{
		if ( ServerPerksMut(Mut) != none )
		{
			break;
		}
	}
	
	n = ServerPerksMut(Mut).LoadPerks.Length;
	
	for(i=0; i<n; i++)
	{
		SRVet = ServerPerksMut(Mut).LoadPerks[i];

		if ( SRVet.default.PerkIndex == CurrentPerkIndex )
		{
			break;
		}
	}
	
	n = class'DKUtil'.default.AvailableWeapons.Length;
	
//	SRVet = GetWeaponPerkFunctionalityType(SRVet,PickupClass);
	
	for(i=0; i<n; i++)
	{
		bNeedLoad = false;

		if ( SRVet.default.PerkIndex != GetWeaponPerkFunctionalityType(SRVet,PickupClass).default.PerkIndex )
		{
		//	bNeedLoad = false;
		}
		if ( CurrentPerkIndex == class<KFWeaponPickup>(class'DKUtil'.default.AvailableWeapons[i]).default.CorrespondingPerkIndex )
		{
			bNeedLoad = true;
		}
		else if ( SRVet.Static.GetPickupShopPerkIndex(SRPlayerReplicationInfo(RelatedActor),class'DKUtil'.default.AvailableWeapons[i]) == CurrentPerkIndex )
		{
			bNeedLoad = true;
		}
		
		if ( bNeedLoad )
		{
			PerkWeapons.Insert(0,1);
			PerkWeapons[0] = class<Weapon>(class'DKUtil'.default.AvailableWeapons[i].default.InventoryType);
		}
	}
	
	MaxRange = 0.00;
	
	while ( PerkWeapons.Length > 0 )
	{
		MaxRange = FMax(MaxRange,GetWeaponRange(PerkWeapons[0],SRVet));
		PerkWeapons.Remove(0,1);
	}
	
	return GetWeaponRange(class<Weapon>(PickupClass.default.InventoryType),SRVet) / MaxRange;
}


static function class<SRVeterancy> GetWeaponPerkFunctionalityType(class<SRVeterancy> SRVet, class<WeaponPickup> PickupClass)
{
	if ( class<RadioBombPickup>(PickupClass) != none )
	{
		return class'SRVetDemolitions';
	}
	else if ( class<WMediShotPickup>(PickupClass) != none )
	{
		return class'SRVetSupportSpec';
	}
	else if ( class<Bal_MP5MPickup>(PickupClass) != none
			|| class<HKMP7LLIPickup>(PickupClass) != none
			|| class<MAC10NewPickup>(PickupClass) != none )
	{
		return class'SRVetSaboteur';
	}

	return SRVet;
}

static function float GetWeaponDamage(class<Weapon> WeaponClass, class<SRVeterancy> SRVet)
{
	local class<WeaponFire> ThisFireClass;
	local class<Projectile> ProjClass;
	local float Ret;
	
	Ret = 0.00;

	if ( class<FN2000AssaultRifle>(WeaponClass) != none || class<SRMeleeGun>(WeaponClass) != none
		|| class<KFMeleeGun>(WeaponClass) != none )
	{
		ThisFireClass = WeaponClass.default.FireModeClass[1];
	}
	else
	{
		ThisFireClass = WeaponClass.default.FireModeClass[0];
	}
	
	if ( class<KFFire>(ThisFireClass) != none )
	{
		Ret = class<KFFire>(ThisFireClass).default.DamageMax;
		if ( SRVet == class'SRVetSharpshooter' || SRVet == class'SRVetGunfighter' )
		{
			Ret *= class<KFWeaponDamageType>(class<KFFire>(ThisFireClass).default.DamageType).default.HeadshotDamageMult;
		}
	}
	else if ( class<SRMeleeFire>(ThisFireClass) != none )
	{
		Ret = class<SRMeleeFire>(ThisFireClass).default.DamageConst + class<SRMeleeFire>(ThisFireClass).default.MaxAdditionalDamage;
	}
	else if ( class<KFMeleeFire>(ThisFireClass) != none )
	{
		Ret = class<KFMeleeFire>(ThisFireClass).default.MeleeDamage;
	}
	else if ( class<KFShotgunFire>(ThisFireClass) != none )
	{
		ProjClass = class<KFShotgunFire>(ThisFireClass).default.ProjectileClass;
		if ( class<FlameTendril1_>(ProjClass) != none )
		{
			Ret = 2.50 * ProjClass.default.Damage;
		}
		else if ( class<FlareGunIgniteProj>(ProjClass) != none )
		{
			Ret = class<FlaregunIgniteProj>(ProjClass).default.Ignites * ProjClass.default.Damage;
		}
		else if ( class<CrossbowArrowNew>(ProjClass) != none )
		{
			Ret = class<CrossbowArrowNew>(ProjClass).default.HeadShotDamageMult * ProjClass.default.Damage;
		}
		else // if ( class<Projectile>(ProjClass) != none )
		{
			Ret = ProjClass.default.Damage * class<KFShotgunFire>(ThisFireClass).default.ProjPerFire;
		}
	}
	
	return Ret;
}

static function float GetWeaponSpeed(class<Weapon> WeaponClass, class<SRVeterancy> SRVet)
{
	local class<WeaponFire> ThisFireClass;
	local float Ret;
	
	Ret = 0.00;

	if ( class<FN2000AssaultRifle>(WeaponClass) != none )
	{
		ThisFireClass = WeaponClass.default.FireModeClass[1];
	}
	else
	{
		ThisFireClass = WeaponClass.default.FireModeClass[0];
	}
	
	Ret = 1.00 / ThisFireClass.default.FireRate;
	
	return Ret;
}

static function float GetWeaponRange(class<Weapon> WeaponClass, class<SRVeterancy> SRVet)
{
	local class<WeaponFire> ThisFireClass;
	local class<Projectile> ProjClass;
	local float Ret;
	
	Ret = 0.00;

	if ( class<FN2000AssaultRifle>(WeaponClass) != none )
	{
		ThisFireClass = WeaponClass.default.FireModeClass[1];
	}
	else
	{
		ThisFireClass = WeaponClass.default.FireModeClass[0];
	}
	
	if ( class<KFMeleeFire>(ThisFireClass) != none )
	{
		Ret = class<KFMeleeFire>(ThisFireClass).default.WeaponRange;
	}
	else if ( class<KFFire>(ThisFireClass) != none )
	{
		Ret = FMax(class<KFFire>(ThisFireClass).default.maxVerticalRecoilAngle,class<KFFire>(ThisFireClass).default.maxHorizontalRecoilAngle)
		* FMax(0.01,class<KFFire>(ThisFireClass).default.RecoilRate);
		Ret = 1.00 / Ret;
	}
	else if ( class<KFShotgunFire>(ThisFireClass) != none )
	{
		ProjClass = class<KFShotgunFire>(ThisFireClass).default.ProjectileClass;

		if ( class<ShotgunBulletDK>(ProjClass) != none || class<TrenchgunNewBullet>(ProjClass) != none )
		{
		//	if ( class<ShotgunBulletDK>(ProjClass).default.bShotgunBullet )
		//	{
				Ret = 1.00 / class<KFShotgunFire>(ThisFireClass).default.Spread;
		//	}
		}
		else if ( ProjClass.default.DamageRadius < 1.00 )
		{
			Ret = FMax(class<KFShotgunFire>(ThisFireClass).default.maxVerticalRecoilAngle,class<KFShotgunFire>(ThisFireClass).default.maxHorizontalRecoilAngle)
			* FMax(0.01,class<KFShotgunFire>(ThisFireClass).default.RecoilRate);
			Ret = 1.00 / Ret;
		}
		else // if ( ProjClass != none )
		{
			Ret = ProjClass.default.DamageRadius;
		}
	}

	Ret = 1.00 / ThisFireClass.default.FireRate;
	
	return Ret;
}

function InitDroppedPickupFor(Inventory Inv)
{
	local KFWeapon W;
	local Inventory InvIt;
	local byte bSaveAmmo[2];
	local int m;

	W = KFWeapon(Inv);

	if (W != None)
	{
		//Check if any other weapon is using the same ammo
		for(InvIt = W.Owner.Inventory; InvIt!=none; InvIt=InvIt.Inventory)
		{
			if(Weapon(InvIt)!=none && InvIt!=W)
			{
				for(m=0; m < 2; m++)
				{
					if(Weapon(InvIt).AmmoClass[m] == W.AmmoClass[m])
						bSaveAmmo[m] = 1;
				}
			}
		}
		if(bSaveAmmo[0]==0)
		{
			MagAmmoRemaining = W.MagAmmoRemaining;
			AmmoAmount[0] = W.AmmoAmount(0);
		}
		if ( bAltAttackSameAmmo )
		{
			AmmoAmount[1] = 0;
		}
		else if(bSaveAmmo[1]==0)
		{
			AmmoAmount[1] = W.AmmoAmount(1);
		}

		SellValue = W.SellValue;
	}
	SetPhysics(PHYS_Falling);
	GotoState('FallingPickup');
	Inventory = Inv;
	bAlwaysRelevant = false;
	bOnlyReplicateHidden = false;
	bUpdateSimulatedPosition = true;
	bDropped = true;
	bIgnoreEncroachers = false; // handles case of dropping stuff on lifts etc
	NetUpdateFrequency = 8;
	bNoRespawn = true;

	if ( KFWeapon(Inventory) != none )
	{
		if ( KFWeapon(Inventory).bIsTier2Weapon )
		{
			if ( !KFWeapon(Inventory).bPreviouslyDropped && PlayerController(Pawn(Inventory.Owner).Controller) != none )
			{
				KFWeapon(Inventory).bPreviouslyDropped = true;
			}
		}
		else
		{
			bPreviouslyDropped = KFWeapon(Inventory).bPreviouslyDropped;
			DroppedBy = PlayerController(Pawn(W.Owner).Controller);
		}
	}
}

defaultproperties
{
	bAltAttackSameAmmo = false
}
